//
//  FindCarTableView.m
//  B段项目
//
//  Created by dllo on 15/9/29.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "FindCarTableView.h"
#import "PrefixHeader.pch"
#import "FindCarTableViewCell.h"
#import "AFNetWorkingW.h"
#import "FindCarModel.h"
#import <MJRefresh.h>

@implementation FindCarTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.dataSource = self;
        self.delegate = self;
        [self registerClass:[FindCarTableViewCell class] forCellReuseIdentifier:@"reuse"];
        self.arr = [NSMutableArray array];
        [self dataHandle];
        [self setupRefresh];
        [self setExtraCellLineHidden:self];
        self.limit = 1;
        
        self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        //        self.activity.center = self.center;
        self.activity.frame = CGRectMake((ScreenWidth - 80) / 2, (ScreenHeight - 120) / 2, 80, 80);
        self.activity.color = RGB(22, 188, 180);
        [self addSubview:self.activity];
        [self.activity startAnimating];

    }
    return self;
}

#pragma mark --cell行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

#pragma mark --cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ScreenHeight * 100 / 667;
}

#pragma mark --cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.model = self.arr[indexPath.row];
    return cell;
}

/* 分割线隐藏 */
- (void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

#pragma mark --点击cell跳转详情界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindCarTableViewCell *cell = [[FindCarTableViewCell alloc] init];
    cell.model = self.arr[indexPath.row];
    
    NSNotificationCenter *center =[ NSNotificationCenter defaultCenter];
    [center postNotificationName:@"pass" object:cell.urlStr];
}

- (void)dataHandle
{
    NSString *urlStr = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getdingyue.php?limit=10&offset=%ld0&type=18534&ver=6.1.6", (long)self.limit];
    self.limit += 1;
    [AFNetWorkingW netWorkingHanderGetGataWithUrl:urlStr resultBlock:^(id result) {
        NSMutableArray *arr = [result objectForKey:@"newsList"];
        for (NSDictionary *dic in arr) {
            FindCarModel *model = [[FindCarModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arr addObject:model];
        }
        [self reloadData];
        [self.footer endRefreshing];
        [self.activity stopAnimating];
    }];
}


#pragma mark ** 刷新
- (void)setupRefresh
{
    /* 上拉刷新 */
    self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)];
    [self.header beginRefreshing];
    /* 下拉加载数据 */
    self.footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)];
    [self.footer beginRefreshing];
}

- (void)headerRefreshing
{
    [self reloadData];
    [self.header endRefreshing];
}

- (void)footerRefreshing
{
    [self reloadData];
    [self.footer endRefreshing];
    [self dataHandle];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
